function searchVendorProduct() {
    $("#pageIndex").val(1);
    searchVendorProductAjaxSubmit();
}

function searchVendorProductAjaxSubmit() {
    $("#vendorProductResult").hide();
    $("#noVendorProductResult").hide();
    $("#vendorProductPaging").html("");
    $("#vendorProductBody").html("");
    $("#checkAllProduct").removeAttr("checked");
    var data = {};
    data.vendorSku = $("#vendorSku").val();
    data.productName = $("#productName").val();
    data.category = $("#category").val();
    data.productStatus = $("#productStatus").val();
    data.pageIndex = $("#pageIndex").val();
    data.pageSize = $("#pageSize").val();
    $.ajax({
        type: 'post',
        url: golbalRootUrl + "vendor/product",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify(data),
        success: function (result) {
            var vendorProducts = result.vendorProducts;
            var total = result.total;
            var pageSize = result.pageSize;
            if (vendorProducts.length > 0) {
                var pageHtml = paging(vendorProducts, total, pageSize);
                $("#vendorProductPaging").html("");
                $(pageHtml).appendTo("#vendorProductPaging");
                var vendorProduct = vendorProducts[0];
                $("#batchActivateButton").html("<a href='javascript:void(0);' onclick=\"batchUpdateVendorProduct('" + vendorProduct.vendorNumber + "'," + true + ")\">Activate</a><span>|</span><a href='javascript:void(0);'  onclick=\"batchUpdateVendorProduct('" + vendorProduct.vendorNumber + "'," + false + ")\">Deactivate</a>");
                for (var i = 0; i < vendorProducts.length; i++) {
                    var vendorProduct = vendorProducts[i];
                    var html = "<tr><td style='width: 60px;'><input type='checkbox' style='margin:3px 2px 0 20px; float:left; vertical-align:middle;' onclick='checkAllCheck();' name='vendorSkuCheckBox' id='vendorSkuCheckBox'  value='" + vendorProduct.vendorSku + "' /></td>" +
                        "<td style='width:150px;'><a href='javascript:void(0);' onClick='showProductDetails(\"" + vendorProduct.vendorNumber + "\",\"" + vendorProduct.vendorSku + "\")'>" + vendorProduct.vendorSku + "</a></td>" +
                        "<td style='width: 350px;'>" + vendorProduct.itemName + "</td>" +
                        "<td style='width:200px;'>" + vendorProduct.categoryName + "</td>" +
                        "<td style='width:100px;' id='productStatus" + vendorProduct.vendorSku + "'>" + vendorProduct.status + "</td>";
                    if (vendorProduct.status == 'I') {
                        html += "<td id='activeButton" + vendorProduct.vendorSku + "'><button onclick=\"simpleActiveVendorProduct('" + vendorProduct.vendorNumber + "','" + vendorProduct.vendorSku + "'," + true + ")\"> Activate</button></td></tr>";
                    }
                    if (vendorProduct.status == 'A') {
                        html += "<td id='activeButton" + vendorProduct.vendorSku + "'><button onclick=\"simpleActiveVendorProduct('" + vendorProduct.vendorNumber + "','" + vendorProduct.vendorSku + "'," + false + ")\">Inactivate</button></td></tr>";
                    }
                    $(html).appendTo("#vendorProductBody");
                    $("#vendorProductBodyList tbody tr:even").addClass("pinkBg");
                }
                $("#vendorProductResult").show();
            } else {
                $("#noVendorProductResult").show();
            }
        }
    });
}

function showProductDetails(vendorNumber, vendorSku) {
    $("#productDetailsInformation").get(0).reset();
    $("#productAttributeListBody").find('tr').remove();
    $("#productAttributeList").hide();
    $("#attributeNameInfo").hide();
    $("#productImag").show();
    var data = {};
    data.vendorNumber = vendorNumber;
    data.vendorSku = vendorSku;
    suryaniHtmlWrapHopupWindow(850, null, '', $('#showProductDetailsPage'), true, null, 20);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: golbalRootUrl + "vendor/product/detail",
        data: JSON.stringify(data),
        success: function (data) {
            var vendorProduct = data.vendorProduct;
            var productPrice = data.productPrice;
            var productAttributes = data.productAttributes;
            var productImage = data.productImage;
            var productCategory = data.productCategory;
            var productCatalog = data.productCatalog;
            //product
            if (undefined != vendorProduct && null != vendorProduct) {
                $("#showVendorNumber").val(vendorProduct.vendorNumber);
                $("#showVendorSku").val(vendorProduct.vendorSku);
                $("#showCatalogName").val(productCategory.categoryName);
                $("#showCategoryName").val(productCatalog.catalogName);
                $("#showSku").val(vendorProduct.sku);
                $("#showParentage").val(vendorProduct.parentage);
                $("#showExternalProductIdType").val(vendorProduct.externalProductIdType);
                $("#showExternalProductId").val(vendorProduct.externalProductId);
                $("#showItemName").val(vendorProduct.itemName);
                $("#showStatus").val(vendorProduct.status);
                $("#showBrandName").val(vendorProduct.brandName);
                $("#showProductType").val(vendorProduct.productType);
                $("#showProductDescription").val(vendorProduct.productDescription);
            }
            var mainImageUrl = "";
            if (undefined != productImage && null != productImage) {
                mainImageUrl = productImage.mainImageUrl;
            }
            $("#productImag").html("<img src='" + mainImageUrl + "' onerror=\"noFind();\" style='width:210px;margin-top:10px;margin-right:60px;'>");

            //price
            if (undefined != productPrice && null != productPrice) {
                var fullYearFormat = "yyyy-MM-dd";
                $("#showListPrice").val(productPrice.listPrice);
                $("#showMapPrice").val(productPrice.mapPrice);
                $("#showStandardPrice").val(productPrice.standardPrice);
                $("#showSalePrice").val(productPrice.salePrice);
                $("#showPreSalePrice").val(productPrice.preSalePrice);
                $("#showCurrency").val(productPrice.currency);
                if (null != productPrice.saleStartDate && "" != productPrice.saleStartDate) {
                    $("#showSaleStartDate").val(format(new Date(Date.parse(productPrice.saleStartDate)), fullYearFormat));
                }
                if (null != productPrice.saleEndDate && "" != productPrice.saleEndDate) {
                    $("#showSaleEndDate").val(format(new Date(Date.parse(productPrice.saleEndDate)), fullYearFormat));
                }
                if (null != productPrice.preSaleStartDate && "" != productPrice.preSaleStartDate) {
                    $("#showPreSaleStartDate").val(format(new Date(Date.parse(productPrice.preSaleStartDate)), fullYearFormat));
                }
                if (null != productPrice.preSaleEndDate && "" != productPrice.preSaleEndDate) {
                    $("#showPreSaleEndDate").val(format(new Date(Date.parse(productPrice.preSaleEndDate)), fullYearFormat));
                }
            }

            //attribute
            $("#productAttributeListBody").html("");
            if (null != productAttributes && productAttributes.length > 0) {
                var str = "";
                for (var i = 0; i < productAttributes.length; i = i + 3) {
                    $("#productAttributeList").show();
                    str += "<tr><td style='width: 300px;'><b>" + productAttributes[i].attributeName + ":</b>" + productAttributes[i].attributeValue + "</td><td style='width: 300px;'>";
                    if (i + 1 < productAttributes.length) {
                        str += "<b>" + productAttributes[i + 1].attributeName + ":</b>" + productAttributes[i + 1].attributeValue;
                    }
                    str += "</td><td style='width: 300px;'>";
                    if (i + 2 < productAttributes.length) {
                        str += "<b>" + productAttributes[i + 2].attributeName + ":</b>" + productAttributes[i + 2].attributeValue;
                    }
                    str += "</td></tr>";
                    $("#productAttributeListBody").html(str);
                    $("#productAttributeList tbody tr:even").addClass("pinkBg");
                }
            } else {
                $("#attributeNameInfo").show();
            }
        }
    });
}

function noFind() {
    var img = event.srcElement;
    var onErrorUrl = golbalRootUrl + "static/images/defaultImg.png";
    img.src = "" + onErrorUrl + "";
    img.onerror = null;
}

function checkAllCheck() {
    if ("checked" != $("input[name='vendorSkuCheckBox']").attr("checked") || true != $("input[name='vendorSkuCheckBox']").attr("checked")) {
        $("#checkAllProduct").removeAttr("checked");
    }
    var flag = 0;
    $("input[name='vendorSkuCheckBox']:checkbox").each(function () {
        if ($(this).attr("checked") != "checked") {
            flag++;
        }
    });
    if (flag == 0) {
        $("#checkAllProduct").attr("checked", true);
    } else {
        $("#checkAllProduct").removeAttr("checked");
    }
}

function batchUpdateVendorProduct(vendorNumber, isActive) {
    $("#selectVendorMsg").hide().removeClass("error");
    var vendorSku = [];
    var subCheck = $("input[name='vendorSkuCheckBox'][checked]");
    $("input[name='vendorSkuCheckBox']").each(function (i) {
        if ("checked" == $(this).attr("checked") || true == $(this).attr("checked")) {
            vendorSku.push($(this).val());
        }
    });
    if (vendorSku.length < 1) {
        $("#selectVendorMsg").html("Please choice at least one product!").addClass("error").show();
        return false;
    }
    var data = {vendorNumber: vendorNumber, vendorSku: vendorSku, activate: isActive};
    activeVendorProduct(data);
}

function simpleActiveVendorProduct(vendorNumber, vendorSku, isActive) {
    var data = {vendorNumber: vendorNumber, vendorSku: [vendorSku], activate: isActive};
    activeVendorProduct(data);
}


function activeVendorProduct(request) {
    $.ajax({
        type: 'post',
        url: golbalRootUrl + "vendor/product/activate",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify(request),
        success: function (data) {
            var msgType = data.msgType;
            if ("success" == msgType) {
                if (request.activate) {
                    for (var i = 0; i < request.vendorSku.length; i++) {
                        $("#activeButton" + request.vendorSku[i]).html("<button onclick=\"simpleActiveVendorProduct('" + request.vendorNumber + "','" + request.vendorSku[i] + "'," + false + ")\"> Inactivate</button>");
                        $("#productStatus" + request.vendorSku[i]).html("A");
                    }
                }
                if (!request.activate) {
                    for (var i = 0; i < request.vendorSku.length; i++) {
                        $("#activeButton" + request.vendorSku[i]).html("<button onclick=\"simpleActiveVendorProduct('" + request.vendorNumber + "','" + request.vendorSku[i] + "'," + true + ")\"> Activate</button>");
                        $("#productStatus" + request.vendorSku[i]).html("I");
                    }
                }
            }
        }
    });
}
function prev() {
    var pageIndex = $("#pageIndex").val();
    $("#pageIndex").val(Number(pageIndex) - Number(1));
    searchVendorProductAjaxSubmit();
}

function next() {
    var pageIndex = $("#pageIndex").val();
    $("#pageIndex").val(Number(pageIndex) + Number(1));
    searchVendorProductAjaxSubmit();
}

function currentPage(currentNum) {
    $("#pageIndex").val(currentNum);
    searchVendorProductAjaxSubmit();
}

function paging(data, total, pageSize) {
    var html = "";
    if (data == "") {
        html = "<div class='errorResultMsg centerBox' style='width:400px;'>No search results found. Please refine your search filters and try again.</div>"
    } else {
        var pageIndex = $("#pageIndex").val();
        var pageNumber = total / pageSize;
        if (pageNumber > 1) {
            pageNumber = Math.ceil(pageNumber);
        } else {
            pageNumber = Math.floor(pageNumber);
        }
        html = "<div class='mt4 clearfix'><span class='font16 pr2'>" + total + "</span><span class='resultsText'>" + 'Results Returned' + "</span><div id='pager' class='pagerControl right'>";
        if (pageNumber > 1) {
            var prevstr = "";
            var nextstr = "";
            var pagestr = "";
            if (pageNumber == pageIndex) {
                prevstr = "<a href='javascript:void(0)' onclick='prev()'>&lt;Prev</a>";
                nextstr = "<span>Next&gt;</span>";
            } else if (pageIndex == 1) {
                prevstr = "<span>&lt;Prev</span>";
                nextstr = "<a href='javascript:void(0)' onclick='next()'>Next&gt;</a>";
            } else {
                prevstr = "<a href='javascript:void(0)' onclick='prev()'>&lt;Prev</a>";
                nextstr = "<a href='javascript:void(0)' onclick='next()'>Next&gt;</a>";
            }
            if (pageNumber > 8) {
                var pagetemp = "";
                for (var i = 1; i <= pageNumber; i++) {
                    if (i == pageIndex) {
                        pagetemp += "<span>" + i + "</span>";
                    } else if (i < pageIndex) {
                        var count = 2;
                        if (pageIndex == pageNumber) {
                            count = 3;
                        }
                        if (pageIndex - i < count) {
                            pagetemp += "<a href='javascript:void(0)' onclick='currentPage(" + i + ")'>" + i + "</a>";
                        }
                    } else {
                        var count = 2;
                        if (pageIndex == 1) {
                            count = 3;
                        }
                        if (i - pageIndex < count) {
                            pagetemp += "<a href='javascript:void(0)' onclick='currentPage(" + i + ")'>" + i + "</a>";
                        }
                    }
                }
                if (pageIndex <= 3) {
                    if (pageIndex == 3) {
                        pagetemp = "<a href='javascript:void(0)' onclick='currentPage(1)'>" + 1 + "</a>" + pagetemp;
                    }
                    pagestr += pagetemp + "......" + "<a href='javascript:void(0)' onclick='currentPage(" + (pageNumber - 1) + ")'>" + (pageNumber - 1) + "</a>" + "<a href='javascript:void(0)' onclick='currentPage(" + pageNumber + ")'>" + pageNumber + "</a>";
                } else if (pageIndex > pageNumber - 3) {
                    if (pageIndex == pageNumber - 2) {
                        pagetemp += "<a href='javascript:void(0)' onclick='currentPage(" + pageNumber + ")'>" + pageNumber + "</a>";
                    }
                    pagestr += "<a href='javascript:void(0)' onclick='currentPage(1)'>" + 1 + "</a>" + "<a href='javascript:void(0)' onclick='currentPage(2)'>" + 2 + "</a>" + "......" + pagetemp;
                } else {
                    pagestr += "<a href='javascript:void(0)' onclick='currentPage(1)'>" + 1 + "</a>" + "..." + pagetemp + "..." + "<a href='javascript:void(0)' onclick='currentPage(" + pageNumber + ")'>" + pageNumber + "</a>";
                }
            } else {
                for (var i = 1; i <= pageNumber; i++) {
                    if (i == pageIndex) {
                        pagestr += "<span>" + i + "</span>";
                    } else {
                        pagestr += "<a href='javascript:void(0)' onclick='currentPage(" + i + ")'>" + i + "</a>";
                    }
                }
            }
            html += prevstr + pagestr + nextstr;
        }
    }
    return html;
}